package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.ShopDetailData;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商家详情数据
 */
public class ShopDetailAdapter extends RecyclerView.Adapter<ShopDetailAdapter.MyViewHolder> {



    private List<ShopDetailData> datas;
    private Context context;

    public ShopDetailAdapter(List<ShopDetailData> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.shop_detail_item, null);

        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        final ShopDetailData data=datas.get(position);

        holder.tvShopAddress.setText(data.getAddress());
        final String phone=data.getComm();
        holder.tvShopPhone.setText(phone);
        holder.tvShopTime.setText(data.getTime1Start()+"-"+data.getTime1End());


        if(mOnItemClickListener !=null){
            holder.llCallToShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onCallPhoneClick(holder,phone);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_shop_phone)
        TextView tvShopPhone;
        @Bind(R.id.ll_call_to_shop)
        LinearLayout llCallToShop;
        @Bind(R.id.tv_shop_address)
        TextView tvShopAddress;
        @Bind(R.id.tv_shop_time)
        TextView tvShopTime;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public static interface OnRecyclerViewItemClickListener {
        void onCallPhoneClick(MyViewHolder holder,String shopPhone );
    }
}
